package edu.cqie.smartfarm.controller;


import edu.cqie.smartfarm.constant.CommonConstant;
import edu.cqie.smartfarm.dto.FarmDTO;
import edu.cqie.smartfarm.dto.FarmQueryDTO;
import edu.cqie.smartfarm.dto.FarmUpdateDTO;
import edu.cqie.smartfarm.result.PageVO;
import edu.cqie.smartfarm.result.R;
import edu.cqie.smartfarm.service.IFarmService;
import edu.cqie.smartfarm.vo.FarmVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyw
 * @since 2024-12-26
 */
@RestController
@RequestMapping("/farm")
@RequiredArgsConstructor
@Api(tags = "牧场管理")
public class FarmController {
    private final IFarmService farmService;

    /**
     * 获取牧场列表
     */
    @ApiOperation(value = "获取牧场列表")
    @GetMapping("/list")
    @Cacheable(value = CommonConstant.PROJECT_NAME+":farmList", key = "'page_'+#farmQueryDTO.pageNumber+'_size_'+#farmQueryDTO.pageSize+'_query_'+#farmQueryDTO")
    public R<PageVO<FarmVO>> getFarmList(@Validated FarmQueryDTO farmQueryDTO){
        PageVO<FarmVO> farmVOList = farmService.getFarmList(farmQueryDTO);
        return R.Success(farmVOList);
    }

    /**
     * 根据id查询牧场详情
     */
    @ApiOperation(value = "根据id查询牧场详情")
    @GetMapping("/{id}")
    @Cacheable(value = CommonConstant.PROJECT_NAME+":farm", key = "#id")
    public R<FarmVO> getFarmById(@PathVariable Integer id){
        FarmVO farmVO = farmService.getFarmVO(id);
        return R.Success(farmVO);
    }

    /**
     * 添加牧场
     */
    @ApiOperation(value = "添加牧场")
    @PostMapping("/add")
    @CacheEvict(value = CommonConstant.PROJECT_NAME+":farmList", allEntries = true)
    public R addFarm(@RequestBody @Validated FarmDTO farmDTO){
        farmService.addFarm(farmDTO);
        return R.Success();
    }

    /**
     * 修改牧场
     */
    @ApiOperation(value = "修改牧场")
    @PutMapping("/update")
    @CacheEvict(value = CommonConstant.PROJECT_NAME+":farmList", allEntries = true)
    public R updateFarm(@RequestBody @Validated FarmUpdateDTO farmUpdateDTO){
        farmService.updateFarm(farmUpdateDTO);
        return R.Success();
    }
}
